<template>
    <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="楼栋名称" prop="">
            <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="所属学院" prop="">
            <el-select v-model="formData.academicianId" placeholder="Select" style="width: 100%">
                <el-option v-for="item in academicianList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
        </el-form-item>
        <el-form-item label="地理位置" prop="">
            <el-input readonly v-model="locationInfo" rows="5" placeholder="请选择楼栋位置" @click="OpenMapLocation" input-style="color:#67C23A"/>
        </el-form-item>
        <div style="text-align: center;margin-top: 20px;">
            <el-button style="margin-right: 50px;" @click="Close3()">取消</el-button>
            <el-button type="primary" @click="SubmitForm()">确定</el-button>
        </div>
    </el-form>
        <!--#region 打卡地图 -->
        <el-dialog v-model="dialogVisible3" title="宇宙无敌霹雳地图定位" width="1000" top="3vh"
        :close-on-press-escape="false">
        <MapLaction v-if="dialogVisible3" :Close="Close3" :transmitBuilding3="formData"></MapLaction>
    </el-dialog>
    <!-- #endregion -->
</template>

<script setup>
import service from '@/request';
import { ElMessage } from 'element-plus';
import { reactive, onMounted, ref, defineProps, computed } from 'vue'
import MapLaction from './MapLaction.vue';
// #region 显示提示
async function ShowTip(isSuccess, msg) {
    if (isSuccess == true) {
        ElMessage({ message: msg, type: 'success' })
    }
    else {
        ElMessage({ message: msg, type: 'error' })
    }
}
//#endregion

let props = defineProps(["Close", "transmitObj"])

let formData = reactive({
    id: 0,
    name: '',
    academicianId:0,
    locationName:'',
    longitude:0,
    latitude:0
})
let locationInfo=computed(()=>{
    if(formData.longitude==0&&formData.latitude==0){
        return ""
    }else{
        return "已定位"
    }
})


//提交
async function SubmitForm() {
    if (formData.name == '' || formData.academicianId == 0|| locationInfo=="") {
        ShowTip(false, '请先完成必要步骤')
        return
    }
    //添加
    if (props.transmitObj == null) {
        await service.post("/Building/addBuilding", formData).then(
            async res => {
                ShowTip(true, "操作成功")
            },
            error => {
                ShowTip(false, error)
            }
        )
    }
    //编辑
    else {
        await service.post("/Building/updateBuilding",formData).then(
            async res => {
                ShowTip(true, "操作成功")
            },
            error => {
                ShowTip(false, error)
            }
        )
    }
    await props.Close()
}


//获取学院数据
let academicianList = reactive([])
async function GetAcademicianData() {
    let resultData = []
    await service.get("/Mangment/getAllAcademician").then(
        (res) => {
            resultData.push(...res.value)
        }
    )
    return resultData
}

//定位地图
let dialogVisible3=ref(false)
async function OpenMapLocation(){
    dialogVisible3.value=true
}
async function Close3(){
    dialogVisible3.value=false
}


onMounted(async () => {
    //如果是编辑操作就初始化
    if (props.transmitObj != null) {
        for (var p in props.transmitObj) {
            formData[p] = props.transmitObj[p]
        }
    }
    academicianList.push({id:0,name:'请选择学院'})
    academicianList.push(...await GetAcademicianData())
})
</script>

<style scoped></style>